iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 15
0
Software Development

學習Go系列 第 15

Go day 15 (flow control)

  • 分享至 

  • xImage
  •  

if else

go 的 if else 條件式不用 (),直接寫條件即可

package main

import (
	"fmt"
)

func main() {
	sum := 51
	if sum > 0 && sum <= 50 {
		fmt.Println("low")
	} else if sum > 50 && sum <= 100 {
		fmt.Println("High")
	} else {
		fmt.Println("error")
	}
}

for

最基礎的 for 迴圈

package main

import (
	"fmt"
)

func main() {
	for i := 0; i < 5; i++ {
		fmt.Print(i)
	}
}

印出

01234

利用 for 迴圈印出陣列裡的元素

var names [3]string = [3]string{"Allen", "Sam", "Daniel"}
for i := 0; i < len(names); i++ {
	fmt.Println(i)
	fmt.Println(names[i])
}

for range

使用 for 迴圈搭配 range 可以取得陣列元素的 index 以及元素的值

package main

import (
	"fmt"
)

func main() {
	var names [3]string = [3]string{"Allen", "Sam", "Daniel"}
	for index, name := range names {
		fmt.Println(index)
		fmt.Println(name)
	}
}

continue & break

使用 continue 或 break 控制迴圈,continue 會直接執行下一個 index,而 break 會停止迴圈

package main

import (
	"fmt"
)

func main() {
	for i := 0; i < 5; i++ {
		if i == 2 {
			continue
		} else if i == 4 {
			break
		}
		fmt.Print(i)
	}
}

印出

013

switch case

如果都沒有符合的條件的話會跑 default,多個條件的 case 可以用 , 隔開

package main

import (
	"fmt"
)

func main() {
	i := 4
	switch i {
	default:
		fmt.Println("default")
	case 1, 4:
		fmt.Println("one")
	case 2:
		fmt.Println("two")
	case 3:
		fmt.Println("three")
	}
}

interface check type

使用 interface 搭配 switch case check type

func checkType(i interface{}) {
	switch t := i.(type) {
	case string:
		fmt.Println("type is string")
	case int:
		fmt.Println("type is int")
	case Counter1:
		fmt.Println("type is Counter1")
	case Counter2:
		fmt.Println("type is Counter2")
	case Counter:
		fmt.Println("type is Counter")
	default:
		fmt.Printf("Don't know type %T\n", t)
	}
}

拿 interface 的範例來 check 看看

package main

import (
	"fmt"
)

type Counter interface {
	Add()
	GetSum() int
}

type Counter1 struct {
	sum int
}

func (c *Counter1) Add() {
	c.sum++
}
func (c *Counter1) GetSum() int {
	return c.sum
}
func (c *Counter1) Sub() {
	c.sum--
}

type Counter2 struct {
	sum int
}

func (c *Counter2) Add() {
	c.sum += 10
}
func (c *Counter2) GetSum() int {
	return c.sum * 2
}
func (c *Counter2) mod() int {
	return c.sum % 2
}

func main() {
	counters := make([]Counter, 2)
	counters[0] = new(Counter1)
	counters[1] = new(Counter2)
	checkType(counters[0]) // type is Counter
	checkType(counters[1]) // type is Counter
	var counter1 Counter1
	var counter2 Counter2
	checkType(counter1) // type is Counter1
	checkType(counter2) // type is Counter2
	checkType(1)        // type is int
	checkType("Daniel") // type is string
	checkType(1.0)      // Don't know type float64

}

func checkType(i interface{}) {
	switch t := i.(type) {
	case string:
		fmt.Println("type is string")
	case int:
		fmt.Println("type is int")
	case Counter1:
		fmt.Println("type is Counter1")
	case Counter2:
		fmt.Println("type is Counter2")
	case Counter:
		fmt.Println("type is Counter")
	default:
		fmt.Printf("Don't know type %T\n", t)
	}
}

sort

有時候常常會利用 for 迴圈排序,但 go 其實已經有一些排序的 function 可以使用了,
像 sort.Strings , sort.Ints 還有像 sort.IntsAreSorted 可以判斷 slice 是否已經排序過了

package main

import (
	"fmt"
	"sort"
)

func main() {
	strs := []string{"daniel", "Andy", "sam", "Jack"}
	sort.Strings(strs)
	fmt.Println("names:", strs)

	ints := []int{1, 5, 3, 4, 0, 3, 9}
	sort.Ints(ints)
	fmt.Println("Ints:   ", ints)

	isSort := sort.IntsAreSorted(ints)
	fmt.Println("Sorted: ", isSort)
}

印出

names: [Andy Jack daniel sam]
Ints:    [0 1 3 3 4 5 9]
Sorted:  true

上一篇
Go day 14 (interface)
下一篇
Go day 16 (goroutines)
系列文
學習Go30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言